home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / IDEBUG.C < prev    next >
C/C++ Source or Header  |  1992-03-18  |  7KB  |  228 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* idebug.c */
  21. /* Debugging support for Ghostscript interpreter */
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "iutil.h"
  25. #include "dict.h"
  26. #include "name.h"
  27. #include "ostack.h"            /* for opdef.h */
  28. #include "opdef.h"
  29. #include "packed.h"
  30. #include "store.h"        /* for make_oper for opdef.h */
  31.  
  32. /* Forward references */
  33. void debug_print_string(P2(const byte *, ushort));
  34. #define debug_print_name(pnref)\
  35.   debug_print_string((pnref)->value.pname->string_bytes,\
  36.              (pnref)->value.pname->string_size)
  37.  
  38. /* Table of type name strings */
  39. static const char *type_strings[] = { type_print_strings };
  40.  
  41. /* Print a ref */
  42. void
  43. debug_print_ref(const ref *pref)
  44. {    unsigned size = r_size(pref);
  45.     ushort elt;
  46.     ref nref;
  47.     dprintf1("(%x)", r_type_attrs(pref));
  48.     elt = *(ushort *)pref;
  49.     if ( elt <= packed_max_full_ref )
  50.       switch ( r_type(pref) )
  51.        {
  52.     case t_array:
  53.       dprintf2("array(%u)0x%lx", size, (ulong)pref->value.refs); break;
  54.     case t_boolean:
  55.       dprintf1("boolean %x", pref->value.index); break;
  56.     case t_color:
  57.       dprintf1("color 0x%lx", (ulong)pref->value.pcolor); break;
  58.     case t_condition:
  59.       dprintf1("condition 0x%lx", (ulong)pref->value.pcond); break;
  60.     case t_device:
  61.       dprintf1("device 0x%lx", (ulong)pref->value.pdevice); break;
  62.     case t_dictionary:
  63.       dprintf3("dict(%u/%u)0x%lx",
  64.            dict_length(pref), dict_maxlength(pref),
  65.            (ulong)pref->value.pdict);
  66.       break;
  67.     case t_file:
  68.       dprintf1("file 0x%lx", (ulong)pref->value.pfile); break;
  69.     case t_gstate:
  70.       dprintf1("gstate 0x%lx", (ulong)pref->value.pgstate); break;
  71.     case t_integer: dprintf1("int %ld", pref->value.intval); break;
  72.     case t_lock:
  73.       dprintf1("lock 0x%lx", (ulong)pref->value.plock); break;
  74.     case t_mark: dprintf("mark"); break;
  75.     case t_mixedarray:
  76.       dprintf2("mixed packedarray(%u)0x%lx", size,
  77.            (ulong)pref->value.packed); break;
  78.     case t_name:
  79.       dprintf2("name(0x%lx#%x)", (ulong)pref->value.pname,
  80.            pref->value.pname->index);
  81.       debug_print_name(pref);
  82.       break;
  83.     case t_null: dprintf("null"); break;
  84.     case t_oparray:
  85.       dprintf1("op_array(0x%x)", size);
  86.       name_index_ref(op_array_nx_table[size - op_def_count], &nref);
  87.       debug_print_name(&nref);
  88.       break;
  89.     case t_operator:
  90.       dprintf1("op(0x%x", size);
  91.       if ( size )
  92.         dprintf1(":%s", (char *)(op_def_table[size]->oname + 1));
  93.       dprintf1(")0x%lx", (ulong)pref->value.opproc);
  94.       break;
  95.     case t_real: dprintf1("real %f", pref->value.realval); break;
  96.     case t_shortarray:
  97.       dprintf2("short packedarray(%u)0x%lx", size,
  98.            (ulong)pref->value.packed); break;
  99.     case t_string:
  100.       dprintf2("string(%u)0x%lx", size, (ulong)pref->value.bytes); break;
  101.     default: dprintf1("type 0x%x", r_type(pref));
  102.        }
  103.     else
  104.       switch ( elt >> packed_type_shift )
  105.        {
  106.     case pt_executable_operator:
  107.       dprintf("<op_name>");
  108.       elt &= packed_int_mask;
  109.       op_index_ref(elt, &nref);
  110.       debug_print_ref(&nref);
  111.       break;
  112.     case pt_integer:
  113.       dprintf1("<int> %d", (elt & packed_int_mask) + packed_min_intval);
  114.       break;
  115.     case pt_literal_name: case pt_literal_name+1:
  116.       dprintf("<lit_name>"); elt &= packed_max_name_index; goto ptn;
  117.     case pt_executable_name: case pt_executable_name+1:
  118.       dprintf("<exec_name>"); elt &= packed_max_name_index;
  119. ptn:      name_index_ref(elt, &nref);
  120.       dprintf2("(0x%lx#%x)", (ulong)nref.value.pname, elt);
  121.       debug_print_name(&nref);
  122.       break;
  123.        }
  124. }
  125.  
  126. /* Print a string */
  127. void
  128. debug_print_string(const byte *chrs, ushort len)
  129. {    ushort i;
  130.     for ( i = 0; i < len; i++ )
  131.         dputc(chrs[i]);
  132. }
  133.  
  134. /* Dump one ref */
  135. void
  136. debug_dump_one_ref(const ref *p)
  137. {    uint attrs = r_type_attrs(p);
  138.     uint btype = r_btype(p);
  139.     static const char *as = attr_print_string;
  140.     const char *ap = as;
  141. #define buf_size 30
  142.     char buf[buf_size + 1];
  143.     uint plen;
  144.     if ( btype >= t_next_index )
  145.         dprintf1("0x%02x?? ", btype);
  146.     else
  147.         dprintf1("%s ", type_strings[btype]);
  148.     for ( ; *ap; ap++, attrs >>= 1 )
  149.       if ( *ap != '.' )
  150.         dputc(((attrs & 1) ? *ap : '-'));
  151.     dprintf2(" 0x%04x 0x%08lx", r_size(p), *(ulong *)&p->value);
  152.     if ( obj_cvs(p, (byte *)buf, buf_size, &plen) >= 0 &&
  153.          ((buf[plen] = 0), strcmp(buf, "--nostringval--"))
  154.        )
  155.         dprintf1(" = %s", buf);
  156. }
  157.  
  158. /* Dump a region of memory containing refs */
  159. void
  160. debug_dump_refs(const ref *from, uint size, const char *msg)
  161. {    const ref *p = from;
  162.     uint count = size;
  163.     if ( size && msg )
  164.         dprintf2("%s at 0x%lx:\n", msg, (ulong)from);
  165.     while ( count-- )
  166.        {    dprintf2("..%04x: 0x%02x ", (uint)p & 0xffff, r_type(p));
  167.         debug_dump_one_ref(p);
  168.         dputc('\n');
  169.         p++;
  170.        }
  171. }
  172.  
  173. /* Dump a region of memory */
  174. void
  175. debug_dump_bytes(const byte *from, const byte *to, const char *msg)
  176. {    const byte *p = from;
  177.     if ( from < to && msg )
  178.         dprintf1("%s:\n", msg);
  179.     while ( p != to )
  180.        {    const byte *q = min(p + 16, to);
  181.         dprintf1("%lx:", (ulong)p);
  182.         while ( p != q ) dprintf1(" %02x", *p++);
  183.         dputc('\n');
  184.        }
  185. }
  186.  
  187. /* Dump an array. */
  188. void
  189. debug_dump_array(const ref *array)
  190. {    const ref_packed *pp;
  191.     unsigned int type = r_type(array);
  192.     uint len;
  193.  
  194.     switch (type)
  195.        {
  196.     default:
  197.         dprintf2 ("%s at 0x%lx isn't an array.\n",
  198.               ((type >= 0 && type < countof(type_strings)) ?
  199.                 type_strings[type] : "????"),
  200.                (ulong)array);
  201.         return;
  202.     case t_oparray:
  203.         /* This isn't really an array, but we'd like to see */
  204.         /* its contents anyway. */
  205.         debug_dump_array(op_array_table.value.refs + op_index(array) -
  206.                  op_def_count);
  207.         return;
  208.     case t_array:
  209.     case t_mixedarray:
  210.     case t_shortarray: 
  211.         ;
  212.        }
  213.  
  214.     /* This "packed" loop works for all array-types. */
  215.     for ( len = r_size (array), pp = array->value.packed;
  216.           len > 0;
  217.           len--, pp = packed_next(pp))
  218.        {    ref temp;
  219.         packed_get(pp, &temp);
  220.         dprintf3("..%04x%c 0x%02x ", 
  221.              (uint)pp & 0xffff,
  222.              ((r_is_packed(pp)) ? '*' : ':'),
  223.              r_type(&temp));
  224.         debug_dump_one_ref(&temp);
  225.         dputc ('\n');
  226.        }
  227. }
  228.